<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>创建一个新的id</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.mongoid.html">MongoId</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongoid.gethostname.html">MongoId::getHostname</a></div>
 <div class="up"><a href="class.mongoid.html">MongoId</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongoid.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoId::__construct</h1>
  <p class="verinfo">(PECL mongo &gt;= 0.8.0)</p><p class="refpurpose"><span class="refname">MongoId::__construct</span> &mdash; <span class="dc-title">创建一个新的id</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongoid.construct-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>MongoId::__construct</strong></span>
    ([ <span class="methodparam"><span class="type">string</span> <code class="parameter">$id</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span>
  ] )</div>

 </div>


 <div class="refsect1 parameters" id="refsect1-mongoid.construct-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>

      <em><code class="parameter">id</code></em>
     </dt>

     <dd>

      <p class="para">
       用于 id 的一个字符串。必须为24个十六进制的字符。如果传入一个无效字符串，构造器将忽略它，并创建一个新的 id 值。
      </p>
     </dd>

       
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongoid.construct-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个新的 id。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mongoid.construct-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1459">
   <p><strong>Example #1 <span class="function"><strong>MongoId::__construct()</strong></span> 例子</strong></p>
   <div class="example-contents"><p>
    这个例子展示了如何创建一个新的 id。
    这很少用到，因为在保存到数据库之前，驱动会为数组自动添加一个id。
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />&nbsp;&nbsp;$id1&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoId</span><span style="color: #007700">();<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$id1</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;</span><span style="color: #0000BB">$id2&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoId</span><span style="color: #007700">();<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$id2</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;</span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
49a7011a05c677b9a916612a
49a702d5450046d3d515d10d
</pre></div>
   </div>
  </div>
  <div class="example" id="example-1460">
   <p><strong>Example #2 参数的例子</strong></p>
   <div class="example-contents"><p>这个例子展示了如何使用 string 的参数来初始化一个指定值的 MongoId。</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$id1&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoId</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;从&nbsp;$id1&nbsp;创建一个新的&nbsp;MongoId<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$id2&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoId</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$id1</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;显示&nbsp;$id1&nbsp;和&nbsp;$id2&nbsp;具有相同的十六进制值<br />&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$id1&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$id2</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
bool(true)
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-mongoid.construct-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="mongoid.tostring.html" class="methodname" rel="rdfs-seeAlso">MongoId::__toString()</a> - 返回该 id 十六进制的表示形式</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.mongoid.html">MongoId</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongoid.gethostname.html">MongoId::getHostname</a></div>
 <div class="up"><a href="class.mongoid.html">MongoId</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
